package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.qf.pojo.DtsCategory;
import com.qf.service.CategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CategorySellVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * projectName: wokepase1
 *
 * @author: 强力
 * time: 2021/9/15 14:24
 * description:
 */
@RestController
@RequestMapping("/category")
@CrossOrigin
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    @RequestMapping("/list")
    public Object categorylist(Integer page,Integer limit,Integer id,String name ,String sort,String order){
        IPage<DtsCategory> categoryIPage=categoryService.findByPage(page,limit,id,name,sort,order);
        //封装返回数据
        Map<String,Object> date=new HashMap<>();
        date.put("total",categoryIPage.getTotal());
        date.put("items",categoryIPage.getRecords());
        return ResponseUtil.ok(date);
    }
    @RequestMapping("l1")
    public Object l1(){

        List<Map<String,Object>> data=new ArrayList<>();
       List<DtsCategory> category=categoryService.findcategory();
        if (category != null) {
            for (DtsCategory dtsCategory : category) {
                    Map<String,Object> map=new HashMap<>();
                    map.put("value",dtsCategory.getId());
                    map.put("label",dtsCategory.getName());
                    data.add(map);
            }
        }
        return  ResponseUtil.ok(data);
    }


    @RequestMapping("/create")
    public Object create(@RequestBody DtsCategory category){
           category= categoryService.insertCategory(category);

            return ResponseUtil.ok(category);

    }

    @RequestMapping("/update")
    public Object update(@RequestBody DtsCategory category){
        category=categoryService.updateCatgory(category);

        return ResponseUtil.ok(category);
    }

    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsCategory category){
        category=categoryService.deleteCategory(category);


      return   ResponseUtil.ok(category);
    }
}
